<?php

namespace Xrfg\Api\Trading\Spot\Console;

use Illuminate\Console\Command;
use Xrfg\Core\Trading\Spot\Models\Pair;

class PairConfig extends Command
{
    protected $signature = 'pair:config';

    protected $description = '从数据库中读取交易对数据，保存到配置文件中';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $pairs = Pair::where('status', '!=', Pair::STATUS_HALT)->get();
        $data = [];
        $pairs->each(function (Pair $pair) use (&$data) {
            $data[$pair->symbol] = [
                'id' => $pair->id,
                'coin_id' => $pair->coin_id,
                'coin_name' => $pair->coin_name,
                'market_id' => $pair->market_id,
                'market_name' => $pair->market_name,
                'price_precision' => $pair->coin_decimals,
                'number_precision' => $pair->market_decimals,
            ];
        });
        $filename = 'pairs.php';
        $template = <<<TPL
<?php

return %s;

TPL;
        $contents = sprintf($template, var_export($data, true));
        $result = file_put_contents(config_path($filename), $contents);
        if (false === $result) {
            $this->error('创建配置失败');
        } else {
            $this->info('创建配置成功');
        }
    }
}
